﻿
using hcache;
using hconnect;
using hdal;
using hentity;
using hq;
using hutil;
using MongoDB.Bson;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace htaskCommon {
    public abstract class AbsSynchro : Synchro
    {
        static CoreTaskParamStatus[] wcsTaskStatus = new CoreTaskParamStatus[] { CoreTaskParamStatus.满放变更,CoreTaskParamStatus.空取 };
        protected short CraneId;
       
        public ICoreTaskServer coreTaskServer = WMSDalFactray.getDal<ICoreTaskServer>();
        public AbsSynchro(int craneId)
        {
            this.CraneId = (short)craneId;
        }

        protected override bool onlyOneTimeAsync()
        {

            time = 400;
            return true;
        }
        protected abstract AbsStatus parse();
        public virtual MyDict<int ,TaskStatusPersistence> getTaskNo(AbsStatus status) {
            return null;
        }
        public override void excute()
        {
            AbsStatus cs = parse();
            if (cs == null)
            {
                return;
            }
            MyDict<int, TaskStatusPersistence> dict = getTaskNo(cs);
            if( dict != null ) {
                updataDB(dict);
            }
        
          
            StatusCache.setStatus(GetType(), cs);
            BsonDocument document = BsonDocument.Parse(ObjStrUtil.mySerializeObject(cs));
            BsonDocument his = StatusCache.getHis(GetType());
            if( his != null ) {
             
               String date = his["time"].AsString.Substring(14 ,2);
    
                if( int.Parse( date) != cs.time.Minute ) {
                    his = null;
                }
            }
            
            List<BsonDocument> defList = cs.getDef(his , document);
            cs.publish(defList);
            StatusCache.addDef(defList);
            StatusCache.setHis(GetType(), document);

        }

        bool isError(int hisStatus)
        {
            foreach ( CoreTaskParamStatus s in wcsTaskStatus)
            {
                if (hisStatus == (int)s)
                {
                    return true;
                }
            }
            return false;

        }

       public virtual void updataDB(MyDict<int, TaskStatusPersistence> dict)
        {
            foreach (KeyValuePair<int, TaskStatusPersistence> kv in dict)
            {
                int taskNo = kv.Key;
                if (taskNo == 0)
                {
                    continue;
                }
             
                TaskStatusPersistence tsp = kv.Value;
                CoreTaskParam param = CompleteParamCache.getCompleteParamByTaskNo(taskNo);
                if (param == null)
                {
                    consoleOnly("无有效任务{1}在站台{0}。", tsp.stn, taskNo);
                    continue;

                }
                int hisStatus = param.status;
                if (isError(hisStatus))
                {
                    consoleOnly("站台{0}的任务{1}状态异常", tsp.stn, taskNo);
                    continue;
                }
                
                int upStatus = tsp.status;
                if (hisStatus < upStatus)
                {
                    tsp.status = upStatus;

                    coreTaskServer.updateTaskStatus(tsp);//顺序不能变
                }
                else
                {
                    console(taskNo + "状态以完成:" + upStatus);
                }
                tsp.t.status = (short)tsp.status;
                

            }
         
        }





    }
}
